package com.testmanager.testmananger.others;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;
import com.testmanager.testmananger.shuiyou.common.dto.SdssbjkRuleScriptDto;
import com.testmanager.testmananger.shuiyou.common.myAnnotation.MyColumn;
import org.reflections.ReflectionUtils;

public class OtherDataSource {
	
	
	  public static final String mysqldriver = "com.mysql.cj.jdbc.Driver"; // mysql数据库的驱动类//com.mysql.cj.jdbc.Driver   com.mysql.jdbc.Driver
	    public static final String oracledriver = "oracle.jdbc.OracleDriver"; // oracles数据库的驱动类
	    public static final String sql2005driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // sqlserver数据库的驱动类
	    public static final String sql2000driver = "net.sourceforge.jtds.jdbc.Driver"; // sqlserver数据库的驱动类
	    
	    public DataSource  dataSource;
	
	public  boolean createDataSource(String key, String driveClass, String url, String username, String password, String databasetype) {
        try {
            try { // 排除连接不上的错误
                Class.forName(driveClass);
                DriverManager.getConnection(url, username, password);// 相当于连接数据库
            } catch (Exception e) {
            //	throw new Exception(e);
                return false;
            }
            @SuppressWarnings("resource")
            DruidDataSource druidDataSource = new DruidDataSource();

            druidDataSource.setName(key);
            druidDataSource.setDriverClassName(driveClass);
            druidDataSource.setUrl(url);
            druidDataSource.setUsername(username);
            druidDataSource.setPassword(password);
            druidDataSource.setInitialSize(50); //初始化时建立物理连接的个数。初始化发生在显示调用init方法，或者第一次getConnection时
            druidDataSource.setMaxActive(200); //最大连接池数量
            druidDataSource.setMaxWait(60000); //获取连接时最大等待时间，单位毫秒。当链接数已经达到了最大链接数的时候，应用如果还要获取链接就会出现等待的现象，等待链接释放并回到链接池，如果等待的时间过长就应该踢掉这个等待，不然应用很可能出现雪崩现象
            druidDataSource.setMinIdle(40); //最小连接池数量
            String validationQuery = "select 1 from dual";
            if("mysql".equalsIgnoreCase(databasetype)) {
                driveClass = mysqldriver;
                validationQuery = "select 1";
            } else if("oracle".equalsIgnoreCase(databasetype)){
                driveClass = oracledriver;
                druidDataSource.setPoolPreparedStatements(true); //是否缓存preparedStatement，也就是PSCache。PSCache对支持游标的数据库性能提升巨大，比如说oracle。在mysql下建议关闭。
                druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(50);
                int sqlQueryTimeout = 15000;
                druidDataSource.setConnectionProperties("oracle.net.CONNECT_TIMEOUT=6000;oracle.jdbc.ReadTimeout="+sqlQueryTimeout);//对于耗时长的查询sql，会受限于ReadTimeout的控制，单位毫秒
            } else if("sqlserver2000".equalsIgnoreCase(databasetype)){
                driveClass = sql2000driver;
                validationQuery = "select 1";
            } else if("sqlserver".equalsIgnoreCase(databasetype)){
                driveClass = sql2005driver;
                validationQuery = "select 1";
            }

            druidDataSource.setTestOnBorrow(true); //申请连接时执行validationQuery检测连接是否有效，这里建议配置为TRUE，防止取到的连接不可用
            druidDataSource.setTestWhileIdle(true);//建议配置为true，不影响性能，并且保证安全性。申请连接的时候检测，如果空闲时间大于timeBetweenEvictionRunsMillis，执行validationQuery检测连接是否有效。
            druidDataSource.setValidationQuery(validationQuery); //用来检测连接是否有效的sql，要求是一个查询语句。如果validationQuery为null，testOnBorrow、testOnReturn、testWhileIdle都不会起作用。
            druidDataSource.setFilters("stat");//属性类型是字符串，通过别名的方式配置扩展插件，常用的插件有：监控统计用的filter:stat日志用的filter:log4j防御sql注入的filter:wall
            druidDataSource.setTimeBetweenEvictionRunsMillis(60000); //配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
            druidDataSource.setMinEvictableIdleTimeMillis(180000); //配置一个连接在池中最小生存的时间，单位是毫秒，这里配置为3分钟180000
            druidDataSource.setKeepAlive(true); //打开druid.keepAlive之后，当连接池空闲时，池中的minIdle数量以内的连接，空闲时间超过minEvictableIdleTimeMillis，则会执行keepAlive操作，即执行druid.validationQuery指定的查询SQL，一般为select * from dual，只要minEvictableIdleTimeMillis设置的小于防火墙切断连接时间，就可以保证当连接空闲时自动做保活检测，不会被防火墙切断

            druidDataSource.setRemoveAbandoned(true); //是否移除泄露的连接/超过时间限制是否回收。
            druidDataSource.setRemoveAbandonedTimeout(3600); //泄露连接的定义时间(要超过最大事务的处理时间)；单位为秒。这里配置为1小时
            druidDataSource.setLogAbandoned(true); ////移除泄露连接发生是是否记录日志

            DataSource createDataSource = (DataSource) druidDataSource;
            druidDataSource.init();
            this.dataSource = createDataSource;
            
           // druidDataSource.get
          //  Map<Object, Object> dynamicTargetDataSources_temp = this.dynamicTargetDataSources;
         //   dynamicTargetDataSources_temp.put(key, createDataSource);// 加入map
          //  setTargetDataSources(dynamicTargetDataSources_temp);// 将map赋值给父类的TargetDataSources
           // super.afterPropertiesSet();// 将TargetDataSources中的连接信息放入resolvedDataSources管理
          //  log.info(key+"数据源初始化成功");
            //log.info(key+"数据源的概况："+druidDataSource.dump());
            return true;
        } catch (Exception e) {
           // log.error(e + "");
            return false;
        }
    }
	
	public static void main1(String[] args) throws SQLException {
		ZipBean zipBean = new ZipBean();
		Field[] fields = zipBean.getClass().getDeclaredFields();
		for(Field field : fields) {
            System.out.println(field.getType());
			System.out.println(field.getName());
			System.out.println("======");
		}
		//System.out.println("===11===");
		String host = "47.97.7.224";
	    int port = 3306;
	    String username = "root";
	    String password = "123456";
	    String key = "test1";
	    String driveClass = mysqldriver;
	    String databasetype = "mysql";
	    String url = "jdbc:mysql://47.97.7.224:3306/test1?useSSL=false&useTimezone=true&serverTimezone=GMT%2B8&useUnicode=true&amp&characterEncoding=utf8";
		OtherDataSource dynamicDataSource = new OtherDataSource();
		Boolean flagBoolean = dynamicDataSource.createDataSource(key, driveClass, url, username, password, databasetype);
		
		//DruidDataSourceFactory.cre
		Connection connection = dynamicDataSource.dataSource.getConnection();
		String sql = "select * from sy_zip;";
		PreparedStatement paPreparedStatement =  connection.prepareStatement(sql);
		ResultSet resultSet = paPreparedStatement.executeQuery();
        while (resultSet.next()){

//            for (Field field : fields){
//                System.out.println(resultSet.getInt(field.getName()));
//            }

            resultSet.getInt("id");
            System.out.println("===="+resultSet.getInt("id"));
            System.out.println(resultSet.getString("name"));
            System.out.println(resultSet.getInt("type"));
            zipBean.setId(resultSet.getInt("id"));
            zipBean.setName(resultSet.getString("name"));
            zipBean.setType(resultSet.getInt("type"));
            System.out.println(zipBean);
        }

		
	}


    public static void getclass(Class clazz){
        Field[] fields = clazz.getDeclaredFields();
        System.out.println(fields.length);
        for (Field field : fields){
            //log.info(field.getName());
            System.out.println("=========");
            System.out.println(field.getName());
            System.out.println(field.getType());
        }

    }

    public static void main(String[] args) throws IllegalAccessException {
        getclass(ZipBean.class);
        Field[] fields = ZipBean.class.getDeclaredFields();
        System.out.println(fields.length);
        ZipBean zipBean = new ZipBean();

        boolean isMy = false;
//        Field[] fields1 = SdssbjkRuleScriptDto.class.getDeclaredFields();
//
//        for (Field field : fields1){
//
//            System.out.println(isMy=field.isAnnotationPresent(MyColumn.class));
//            System.out.println(isMy?field.getAnnotation(MyColumn.class).name():"不存在此column");
//           // field.set(zipBean,"");
//
//        }
        Map map = new HashMap();
        map.put("id",1);
        map.put("name","sfdf");
      // map.put("type",33);
        boolean accessible = false;
        for (Field field : fields){
            accessible = field.isAccessible();
            field.setAccessible(true);//取消访问检察
            Class o = field.getType();
           // field.get
            Object value = map.get(field.getName());
            System.out.println(Objects.equals(o,int.class));
            if (Objects.equals(o,int.class)|| Objects.equals(o,long.class)||Objects.equals(o,float.class)||Objects.equals(o,short.class)){
                value = null == value ?0:value;
            }
            field.set(zipBean,value);
            field.setAccessible(accessible);
           // ReflectionUtils.
        }
        System.out.println(zipBean);


        String sql2 = String.format("select *from sdssbjk_rule_script  t where t.ID=%s","30000000055");
        System.out.println(sql2);


       // System.out.println(Objects.equals(int.class,Integer.class));


    }

	
	
	
}
